package br.ucb.rest.persistencia;

import javax.persistence.EntityManagerFactory;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public final class HibernateListener implements ServletContextListener {

	private static EntityManagerFactory factory;
	
    public HibernateListener() {
        super();
    }

	public void contextInitialized(ServletContextEvent event) {
        try {
        	if (factory == null)
            	factory = HibernateUtil.getInstance().getEntityManagerFactory();
        }catch (Exception e) {
			e.printStackTrace();
			System.err.println("CAUSA DO ERRO: \n");
			e.getCause().printStackTrace();
		}
    }

	public void contextDestroyed(ServletContextEvent event) {
        if (factory != null)
        	factory.close();
    }
	
}
